package com.castle.fortress.admin.oa.rest;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.oa.entity.AttendanceSettingEntity;
import com.castle.fortress.admin.oa.dto.AttendanceSettingDto;
import com.castle.fortress.admin.system.entity.SysUser;
import com.castle.fortress.admin.oa.service.AttendanceSettingService;
import com.castle.fortress.common.utils.ConvertUtil;
import com.castle.fortress.admin.utils.WebUtil;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.util.Strings;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 考勤规则设置 api 控制器
 *
 * @author whc
 * @since 2022-11-03
 */
@Api(tags="考勤规则设置api管理控制器")
@RestController
@RequestMapping("/api/oa/attendanceSetting")
public class ApiAttendanceSettingController {
    @Autowired
    private AttendanceSettingService attendanceSettingService;


    /**
     * 考勤规则设置的分页展示
     * @param attendanceSettingDto 考勤规则设置实体类
     * @param currentPage 当前页
     * @param size  每页记录数
     * @return
     */
    @ApiOperation("考勤规则设置分页展示")
    @GetMapping("/page")
    public RespBody<IPage<AttendanceSettingDto>> pageAttendanceSetting(AttendanceSettingDto attendanceSettingDto, @RequestParam(required = false) Integer currentPage, @RequestParam(required = false)Integer size){
        Integer pageIndex= currentPage==null? GlobalConstants.DEFAULT_PAGE_INDEX:currentPage;
        Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
        Page<AttendanceSettingDto> page = new Page(pageIndex, pageSize);

        IPage<AttendanceSettingDto> pages = attendanceSettingService.pageAttendanceSetting(page, attendanceSettingDto);
        return RespBody.data(pages);
    }

    /**
     * 考勤规则设置保存
     * @param attendanceSettingDto 考勤规则设置实体类
     * @return
     */
    @ApiOperation("考勤规则设置保存")
    @PostMapping("/save")
    public RespBody<String> saveAttendanceSetting(@RequestBody AttendanceSettingDto attendanceSettingDto){
        if(attendanceSettingDto == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            AttendanceSettingEntity attendanceSettingEntity = ConvertUtil.transformObj(attendanceSettingDto,AttendanceSettingEntity.class);
        if(attendanceSettingService.save(attendanceSettingEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 考勤规则设置编辑
     * @param attendanceSettingDto 考勤规则设置实体类
     * @return
     */
    @ApiOperation("考勤规则设置编辑")
    @PostMapping("/edit")
    public RespBody<String> updateAttendanceSetting(@RequestBody AttendanceSettingDto attendanceSettingDto){
        if(attendanceSettingDto == null || attendanceSettingDto.getId() == null || attendanceSettingDto.getId().equals(0L)){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            AttendanceSettingEntity attendanceSettingEntity = ConvertUtil.transformObj(attendanceSettingDto,AttendanceSettingEntity.class);
        if(attendanceSettingService.updateById(attendanceSettingEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 考勤规则设置删除
     * @param ids 考勤规则设置id集合
     * @return
     */
    @ApiOperation("考勤规则设置删除")
    @PostMapping("/delete")
    public RespBody<String> deleteAttendanceSetting(@RequestBody List<Long> ids){
        if(ids == null || ids.isEmpty()){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(attendanceSettingService.removeByIds(ids)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 考勤规则设置详情
     * @param id 考勤规则设置id
     * @return
     */
    @ApiOperation("考勤规则设置详情")
    @GetMapping("/info")
    public RespBody<AttendanceSettingDto> infoAttendanceSetting(@RequestParam Long id){
        if(id == null){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            AttendanceSettingEntity attendanceSettingEntity = attendanceSettingService.getById(id);
            AttendanceSettingDto attendanceSettingDto = ConvertUtil.transformObj(attendanceSettingEntity,AttendanceSettingDto.class);
        return RespBody.data(attendanceSettingDto);
    }


}
